package path;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;

public class TrailPath extends Path{
	long lengthMs;
	
	public TrailPath(PApplet pa, long lengthMs) {
		super(pa);
		this.lengthMs = lengthMs;
	}
	
	public void addFrame(KeyFrame kf){
		super.addFrame(kf);
		
		while(getLength() > lengthMs){
			removeKeyFrame(0);
		}
	}
	
	public void addFrames(ArrayList l){
		Iterator i = l.iterator();
		while(i.hasNext()){
			addFrame((KeyFrame)i.next());
		}
	}
	public void draw(PApplet pa){
		Iterator i = keyFrames.iterator();
		while(i.hasNext()){	
			setVisibility(System.currentTimeMillis() - startTime, (KeyFrame)i.next(), lengthMs);
		}
		super.draw(pa);
	}
	
	private void setVisibility(long currentFrame, KeyFrame kf, long trailLength){
		long diff = currentFrame - (kf.getTime());
		
		if(diff > trailLength || diff < 0){
			kf.setVisibility(0f);
			return;
		}
		
		kf.setVisibility((float)Math.cos((float)diff / (float)trailLength));
	}

}
